package tgc.edu.library.web.form;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import tgc.edu.library.custom.BaseForm;
import tgc.edu.library.entity.Record;


public class RecordForm extends BaseForm<Integer>{

	private String username;//操作人用户名
	private String date; //操作时间
	private String operation;//操作事情
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	public Specification<Record> logBuildSpes() {
	Specification<Record> spec = new Specification<Record>() {
		
		@Override
		public Predicate toPredicate(Root<Record> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
			
			System.out.println("进来查询了"+username);
			HashSet<Predicate> rules = new HashSet<>();
			if(StringUtils.hasText(username)) {
				rules.add(criteriaBuilder.like(root.get("username"), "%"+username+"%"));
				
			}else if(StringUtils.hasText(date)){
				rules.add(criteriaBuilder.like(root.get("date"), "%"+date+"%"));
			}
			else if(StringUtils.hasText(operation)){
				rules.add(criteriaBuilder.like(root.get("operation"), "%"+operation+"%"));
			}
			return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
		}
		
	};
	return spec;
	}
}
